package com.ericsson.enap.modules.monitor.job;

import com.ericsson.common.quartz.job.MainJob;
import com.ericsson.enap.modules.topology.entity.EpcCabVlanDict;
import com.ericsson.enap.modules.monitor.service.IEpcCabVlanDictService;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:第一个测试job
 * @author: wangjingshi
 * @create: 2020-12-07 09:13
 */
@Component
public class LoadVlanExcelJob implements MainJob {

    @Autowired
    private IEpcCabVlanDictService epcCabVlanDictService;

    @Override
    public void doTask() {
        XSSFSheet sheet = ExcelData("E://VLAN_LIST.xlsx", "VLAN");
        int rows = sheet.getPhysicalNumberOfRows();
        List<EpcCabVlanDict> innerList;
        Map<String,List<EpcCabVlanDict>> innerMap;
        Map<String,Map<String,List<EpcCabVlanDict>>> outerMap = new HashMap<String,Map<String,List<EpcCabVlanDict>>>();
        EpcCabVlanDict epcCabVlanDict;
        for (int i = 1; i < rows ; i++) {
            XSSFRow row = sheet.getRow(i);
            String VLAN_CATEGORY = row.getCell(0).toString();
            XSSFCell cell1 = row.getCell(1);
            String VLAN_VNF_CATEGORY = cell1==null?VLAN_CATEGORY:cell1.toString();
            row.getCell(2).setCellType(CellType.STRING);
            String VLAN_ID = String.valueOf(row.getCell(2));
            XSSFCell cell3 = row.getCell(3);
            String EpcCabVlanDict_DESCRIPTION = cell3==null?VLAN_ID:cell3.toString();
            epcCabVlanDict = new EpcCabVlanDict();
            epcCabVlanDict.setName(VLAN_ID);
            epcCabVlanDict.setValue(EpcCabVlanDict_DESCRIPTION);
            if(outerMap.containsKey(VLAN_CATEGORY)){
                innerMap = outerMap.get(VLAN_CATEGORY);
                if(innerMap.containsKey(VLAN_VNF_CATEGORY)){
                    innerList = innerMap.get(VLAN_VNF_CATEGORY);
                    innerList.add(epcCabVlanDict);
                }else{
                    innerList = new ArrayList<>();
                    innerList.add(epcCabVlanDict);
                    innerMap.put(VLAN_VNF_CATEGORY,innerList);
                }
            }else{
                innerList = new ArrayList<>();
                innerMap = new HashMap<String,List<EpcCabVlanDict>>();
                innerList.add(epcCabVlanDict);
                innerMap.put(VLAN_VNF_CATEGORY,innerList);
                outerMap.put(VLAN_CATEGORY,innerMap);
            }
        }

        for (Map.Entry<String, Map<String,List<EpcCabVlanDict>>> outer_entry : outerMap.entrySet()) {
            innerMap = outer_entry.getValue();
            epcCabVlanDict = new EpcCabVlanDict();
            epcCabVlanDict.setPid(0);
            epcCabVlanDict.setName(outer_entry.getKey());
            epcCabVlanDict.setValue(outer_entry.getKey());
            int outer_id = epcCabVlanDictService.insertEpcCabVlanDict(epcCabVlanDict);
            for(Map.Entry<String,List<EpcCabVlanDict>> inner_entry:innerMap.entrySet()){
                innerList = inner_entry.getValue();
                epcCabVlanDict = new EpcCabVlanDict();
                epcCabVlanDict.setPid(outer_id);
                epcCabVlanDict.setName(inner_entry.getKey());
                epcCabVlanDict.setValue(inner_entry.getKey());
                int inner_id = epcCabVlanDictService.insertEpcCabVlanDict(epcCabVlanDict);
                for (int i = 0; i < innerList.size(); i++) {
                    epcCabVlanDict = innerList.get(i);
                    epcCabVlanDict.setPid(inner_id);
                    epcCabVlanDictService.insertEpcCabVlanDict(epcCabVlanDict);
                }
            }
        }
    }


    public XSSFSheet ExcelData(String filePath,String sheetName){
        FileInputStream fileInputStream = null;
        XSSFSheet sheet = null;
        try {
            fileInputStream = new FileInputStream(filePath);
            XSSFWorkbook sheets = new XSSFWorkbook(fileInputStream);
            //获取sheet
            sheet = sheets.getSheet(sheetName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sheet;
    }
}
